﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;


namespace tmAsk.Data
{
    public class Fileuploads
    {
        public static bool createfile(string path)
        {
            try
            {
                if (!File.Exists(path))
                {
                    File.Create(path).Close();
                    return true;
                }
                File.Delete(path);
                File.Create(path).Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static int DeleteFile(string path)
        {
            FileInfo info = new FileInfo(HttpContext.Current.Server.MapPath(path));
            if (info.Exists)
            {
                if (info.IsReadOnly)
                {
                    return -1;
                }
                info.Delete();
                return 1;
            }
            return 0;
        }

        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
        {
            Image originalImage = Image.FromFile(originalImagePath);
            int towidth = width;
            int toheight = height;
            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;
            if (originalImage.Width > originalImage.Height)
            {
                toheight = (originalImage.Height * width) / originalImage.Width;
            }
            else
            {
                towidth = (originalImage.Width * height) / originalImage.Height;
            }
            //新建一个bmp图片
            Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
            //新建一个画板
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),new Rectangle(x, y, ow, oh),GraphicsUnit.Pixel);
            originalImage.Dispose();
            string imageType = "jpg";
            try
            {
                //以jpg格式保存缩略图
                switch (imageType.ToLower())
                {
                    case "gif":
                        bitmap.Save(thumbnailPath, ImageFormat.Gif);
                        break;
                    case "jpg":
                        bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
                        break;
                    case "bmp":
                        bitmap.Save(thumbnailPath, ImageFormat.Bmp);
                        break;
                    case "png":
                        bitmap.Save(thumbnailPath, ImageFormat.Png);
                        break;
                    default:
                        bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
                        break;
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                bitmap.Dispose();
                g.Dispose();
            } 
        }
        
        /*
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
        {
            try
            {
                Image image = Image.FromFile(originalImagePath);
                int num = width;
                int num2 = height;
                int x = 0;
                int y = 0;
                int num5 = image.Width;
                int num6 = image.Height;
                if (image.Width > image.Height)
                {
                    num2 = (image.Height * width) / image.Width;
                }
                else
                {
                    num = (image.Width * height) / image.Height;
                }
                Image image2 = new Bitmap(num, num2);
                Graphics graphics = Graphics.FromImage(image2);
                graphics.InterpolationMode = InterpolationMode.High;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.Clear(Color.Transparent);
                graphics.DrawImage(image, new Rectangle(0, 0, num, num2), new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
                image.Dispose();
                image2.Save(thumbnailPath, ImageFormat.Jpeg);
                image2.Dispose();
                graphics.Dispose();
            }
            catch
            {
            }
        }
        */

        public int SearchFile(string path, string fileName)
        {
            DirectoryInfo info = new DirectoryInfo(HttpContext.Current.Server.MapPath(path));
            foreach (FileInfo info2 in info.GetFiles())
            {
                if (info2.Name.IndexOf(fileName) != -1)
                {
                    return 1;
                }
            }
            return 0;
        }

        public static string Upload(string path, int Filesize, HttpPostedFile UpFile, string docType)
        {
            string str = ((DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()) + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()) + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            string fileName = UpFile.FileName;
            string str3 = fileName.Substring(fileName.LastIndexOf(".") + 1);
            string[] strArray = docType.Split(new char[] { ',' });
            bool flag = false;
            switch (fileName)
            {
                case "":
                case null:
                    return "-1";
            }
            if (UpFile.ContentLength > (Filesize * 0x400))
            {
                return "-2";
            }
            foreach (string str5 in strArray)
            {
                if (str5.ToLower().IndexOf(str3) != -1)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                return "-3";
            }
            DirectoryInfo info = new DirectoryInfo(HttpContext.Current.Server.MapPath(path));
            if (!info.Exists)
            {
                info.Create();
            }
            path = path + str + "." + str3;
            UpFile.SaveAs(HttpContext.Current.Server.MapPath(path));
            try
            {
                if (Image.FromFile(HttpContext.Current.Server.MapPath(path)).Width <= 0)
                {
                    DeleteFile(path);
                    return "-4";
                }
            }
            catch
            {
                DeleteFile(path);
                return "-4";
            }
            return path;
        }

        public static string Upload(string path, int smallwidth, int Filesize, HttpPostedFile UpFile, string docType)
        {
            string str = ((DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()) + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()) + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            string str2 = ",image/bmp,image/gif,image/jpeg,image/pjpeg,image/x-png,";
            if (!str2.Contains("," + UpFile.ContentType + ","))
            {
                return "请选择正规的图片";
            }
            int contentLength = UpFile.ContentLength;
            Image image = new Bitmap(UpFile.InputStream);
            string fileName = UpFile.FileName;
            string str5 = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
            string str6 = "";
            switch (fileName)
            {
                case "":
                case null:
                    return "上传失败,请选择图片";
            }
            if (contentLength > (Filesize * 0x400))
            {
                return ("上传失败,文件太大超过了" + (Filesize * 0x400));
            }
            if (docType.ToLower().IndexOf(str5) < 0)
            {
                return "上传失败,文件类型不正确";
            }
            DirectoryInfo info = new DirectoryInfo(HttpContext.Current.Server.MapPath(path));
            if (!info.Exists)
            {
                info.Create();
            }
            str6 = path + str + "." + str5;
            if (image.Width > 0)
            {
                int width = 0;
                int height = 0;
                if (image.Width > smallwidth)
                {
                    width = smallwidth;
                    height = (smallwidth * image.Height) / image.Width;
                }
                else
                {
                    width = image.Width;
                    height = image.Height;
                }
                Bitmap bitmap = new Bitmap(width, height);
                Graphics graphics = Graphics.FromImage(bitmap);
                graphics.InterpolationMode = InterpolationMode.High;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.Clear(Color.Transparent);
                graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                bitmap.Save(HttpContext.Current.Server.MapPath(str6), ImageFormat.Jpeg);
                graphics.Dispose();
                bitmap.Dispose();
                return str6;
            }
            image.Dispose();
            DeleteFile(path);
            return "上传失败,文件非法格式";
        }

        public static string Upload(string path, string smallpath, int smallwidth, int smallheight, int Filesize, HttpPostedFile UpFile, string docType)
        {
            string str = ((DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()) + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()) + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            string str2 = ",image/bmp,image/gif,image/jpeg,image/pjpeg,image/x-png,";
            if (!str2.Contains("," + UpFile.ContentType + ","))
            {
                return "-3";
            }
            string fileName = UpFile.FileName;
            string str4 = fileName.Substring(fileName.LastIndexOf(".") + 1);
            string[] strArray2 = docType.Split(new char[] { ',' });
            bool flag = false;
            string str5 = "";
            switch (fileName)
            {
                case "":
                case null:
                    return "0";
            }
            if (UpFile.ContentLength > (Filesize * 0x400))
            {
                return "1";
            }
            foreach (string str7 in strArray2)
            {
                if (str7.ToLower().IndexOf(str4) != -1)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                return "2";
            }
            string str8 = HttpContext.Current.Server.MapPath(path);
            string str6 = HttpContext.Current.Server.MapPath(smallpath);
            DirectoryInfo info2 = new DirectoryInfo(str8);
            if (!info2.Exists)
            {
                info2.Create();
            }
            DirectoryInfo info = new DirectoryInfo(str6);
            if (!info.Exists)
            {
                info.Create();
            }
            path = path + str + "." + str4;
            str5 = str + "." + str4;
            UpFile.SaveAs(HttpContext.Current.Server.MapPath(path));
            try
            {
                Image image = Image.FromFile(HttpContext.Current.Server.MapPath(path));
                if (image.Width > 0)
                {
                    image.Dispose();
                    string originalImagePath = HttpContext.Current.Server.MapPath(path);
                    string thumbnailPath = HttpContext.Current.Server.MapPath(smallpath + str + "." + str4);
                    MakeThumbnail(originalImagePath, thumbnailPath, smallwidth, smallheight);
                }
                else
                {
                    image.Dispose();
                    DeleteFile(path);
                    return "3";
                }
            }
            catch
            {
                DeleteFile(path);
                return "3";
            }
            return str5;
        }

        public static string UploadFile(string path, int Filesize, HttpPostedFile UpFile, string docType)
        {
            string str = ((DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()) + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()) + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            string fileName = UpFile.FileName;
            string str3 = ",image/bmp,image/gif,image/jpeg,image/pjpeg,image/x-png,";
            if (!str3.Contains("," + UpFile.ContentType + ","))
            {
                return "-3";
            }
            string str6 = fileName.Substring(fileName.LastIndexOf(".") + 1);
            string[] strArray2 = docType.Split(new char[] { ',' });
            bool flag = false;
            switch (fileName)
            {
                case "":
                case null:
                    return "-1";
            }
            if (UpFile.ContentLength > (Filesize * 0x400))
            {
                return "-2";
            }
            foreach (string str5 in strArray2)
            {
                if (str5.ToLower().IndexOf(str6) != -1)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                return "-3";
            }
            DirectoryInfo info = new DirectoryInfo(HttpContext.Current.Server.MapPath(path));
            if (!info.Exists)
            {
                info.Create();
            }
            path = path + str + "." + str6;
            UpFile.SaveAs(HttpContext.Current.Server.MapPath(path));
            return path;
        }
    }

}